//
//  UIImage+ImageCircle.m
//  Image
//
//  Created by kang on 2018/7/12.
//  Copyright © 2018年 fuyunhe. All rights reserved.
//

#import "UIImage+ImageCircle.h"


@interface view : UIView

@property(nonatomic,retain)UIImage *image;

@end

@implementation view

- (void)drawRect:(CGRect)rect{
    
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    
    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, rect.size.width / 2, rect.size.height / 2));
    CGContextClip(ctx);
    CGContextFillPath(ctx);
    
    [_image drawAtPoint:CGPointMake(0, 0)];
    
    CGContextRestoreGState(ctx);
}

@end


@implementation UIImage (ImageCircle)

-(UIImage *)imageClipCircle{
    
    CGFloat imageSizeMin = MIN(self.size.width, self.size.height);
    CGSize imageSize = CGSizeMake(imageSizeMin, imageSizeMin);
    
    view *mView = [[view alloc] init];
    mView.image = self;
    
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    mView.frame = CGRectMake(0, 0, imageSizeMin, imageSizeMin);
    mView.backgroundColor = [UIColor whiteColor];
    [mView.layer renderInContext:context];
    UIImage *imgNew = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imgNew;
}


@end
